package controllers

import (
	"read.com/admin/models"
	"encoding/base64"
	"read.com/admin/libs"
)

type UserController struct {
	BaseController
}

func (self *UserController) List() {
	self.Data["pageTitle"] = "用户列表"
	total := models.GetTotalCount()
	today := models.GetTodayCount()
	totalVip := models.GetVipUserCount()
	todayVip := models.GetTodayVipCount()
	self.Data["total"] = total
	self.Data["today"] = today
	self.Data["totalVip"] = totalVip
	self.Data["todayVip"] = todayVip
	self.display()
}

func (self *UserController) Table() {
	userId,_ := self.GetInt("userId")
	name := base64.StdEncoding.EncodeToString([]byte(self.GetString("name")))
	//列表参数
	page, err := self.GetInt("page")
	if err != nil {
		page = 1
	}
	limit, err := self.GetInt("limit")
	if err != nil {
		limit = 10
	}
	//result,count := models.GetUserList(page,limit,userId,name)

	// 是查询还是导出
	result, count, tableHead := models.GetUserList(page,limit,userId,name)
	export := self.GetString("export")
	if export == "1" { // 导出excel格式
		libs.FormatExcel(self.Ctx.Output, result,tableHead)
	} else { // 查询
		self.ajaxList("成功", MSG_OK, count, result)
	}
}

func (self *UserController) Give() {
	userId := self.GetString("userId")
	appId,_ := self.GetInt("appId")
	itemType,_ := self.GetInt("type")
	num,_ := self.GetInt("number")
	reason:= self.GetString("reason")
	err := models.GiveUserPresent(userId ,appId ,itemType ,num,reason)
	if err != nil {
		self.ajaxMsg("失败", MSG_ERR)
	}else {
		self.ajaxMsg("成功", MSG_OK)
	}
}

